<template>
  <div class="app-container-flex" v-show="!modifyDeliverInfo.visible">
    <NewFullScreen v-model="showDialog" title="订单明细">
      <template v-slot:full-button>
        <el-button v-if="infoList.data.status === 1" @click="shipment"
          >发货</el-button
        >
        <el-button
          v-if="
            type !== 'pendDealAssetDeliverOrder' && infoList.data.status === 2
          "
          @click="invalid"
          >作废</el-button
        >
        <el-button
          v-if="
            (infoList.data.status === 2 || infoList.data.status === 3) &&
            type === 'pendDealAssetDeliverOrder' &&
            $permission('pendDealAssetDeliverOrder:receiving')
          "
          @click="receiving"
          >收货</el-button
        >

        <el-button @click="printProductCode">打印产品码</el-button>
        <el-button v-if="infoList.data.status !== 1" @click="print"
          >打印</el-button
        >
        <!-- <el-button @click="excel">导出excel</el-button> -->
      </template>
      <div class="model-panel">
        <div class="model-panel-title">
          <p>{{ $t('basic_information') }}</p>
        </div>
        <div class="model-panel-body">
          <BasicInformation
            :info="infoList"
            :label-width="infoList.labelWidth"
          />
        </div>
      </div>

      <div class="model-panel">
        <div class="model-panel-title">
          <p>{{ $t('detailInfo') }}</p>
        </div>
        <div class="model-panel-body">
          <div class="flex_full_item">
            <!-- 表格 -->
            <PageTable
              :refresh="tableInfo.refresh"
              :init-curpage="tableInfo.initCurpage"
              v-model:data="tableInfo.data"
              :pager="false"
              :summary="true"
              :api="mergeDeliverOrderDetail"
              :summary-field-list="tableInfo.summaryFieldList"
              :check-box="false"
              :tab-index="true"
              v-model:query="filterInfo.query"
              :field-list="tableInfo.fieldList"
              :isDynamic="true"
              @handleEvent="handleEvent"
            >
            </PageTable>
          </div>
        </div>
      </div>
    </NewFullScreen>

    <ChosePrinter :visible="printerInfo.visible" @callBack="printerCallBack" />
  </div>
  <ModifyOrder
    v-if="modifyDeliverInfo.visible"
    v-model:visible="modifyDeliverInfo.visible"
    :title="modifyDeliverInfo.title"
    :deliver-id="modifyDeliverInfo.deliverId"
    :receivingSourceOrderNo="modifyDeliverInfo.receivingSourceOrderNo"
    :order-id="modifyDeliverInfo.orderId"
    @returnModify="returnBcak"
  ></ModifyOrder>
</template>

<script setup name="deliverOrderDetail">
import {
  getDeliverOrderById,
  mergeDeliverOrderDetail,
  shipmentsGoods,
  deliverOrderProductCodePrint,
  deliverOrderDetailInvalid,
  deliverOrderValidateLicense,
} from '@/api/supplier.js'
import ModifyOrder from '@/views/asset/assetManage/assetReceiving/modifyByDeliver'

const { proxy } = getCurrentInstance()
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  id: {
    type: [Number, String],
  },
  type: {
    type: String,
  },
})
const infoList = ref({
  data: {},
  fieldList: [
    { label: '发货单号', value: 'orderNo', type: 'text' },
    { label: '销售单号', value: 'saleNo', type: 'text' },
    { label: '采购单号', value: 'purchNo', type: 'text' },
    { label: '发票号码', value: 'invoiceNumber', type: 'text' },
    { label: '货单号码', value: 'waybillNumber', type: 'text' },
    { label: '客户', value: 'hospitalName', type: 'text' },
    { label: '客户部门', value: 'deptName', type: 'text' },
    { label: '预计发货日期', value: 'expectedDeliverTime', type: 'text' },
    { label: '收货地址', value: 'receiveAddressValue', type: 'text' },
    { label: '收货联系人', value: 'receiveContacts', type: 'text' },
    { label: '联系人电话', value: 'receivePhone', type: 'text' },
    { label: '创建人', value: 'createUserName', type: 'text' },
    { label: '创建时间', value: 'createTime', type: 'text' },
    { label: '状态', value: 'statusValue', type: 'text' },
    {
      label: '备注',
      value: 'remark',
      type: 'popover',
    },
  ],
  labelWidth: '130px',
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    orderId: props.id,
  },
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  summaryFieldList: [
    { value: 'qty' },
    { value: 'receiveQty' },
    { value: 'refuseQty' },
    { value: 'sumAmount' },
  ],
  selections: [],
  data: [],
  fieldList: [
    {
      label: '设备名称',
      value: 'commonName',
    },
    {
      label: '设备编码',
      value: 'productCode',
    },
    {
      label: '设备规格',
      value: 'standard',
    },
    {
      label: '设备品牌',
      value: 'trademark',
    },
    {
      label: '厂家名称',
      value: 'brandName',
    },
    {
      label: '设备类别',
      value: 'deviceTypeValue',
    },
    {
      label: '单价',
      value: 'price',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethodAsset,
    },
    {
      label: '发货数量',
      value: 'qty',
      type: 'column',
    },
    {
      label: '收货数量',
      value: 'receiveQty',
      type: 'column',
    },
    {
      label: '拒收数量',
      value: 'refuseQty',
      type: 'column',
    },
    {
      label: '总金额',
      value: 'sumAmount',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethodAsset,
    },
  ],
})
const printerInfo = ref({
  visible: false,
  data: {},
})
const modifyDeliverInfo = ref({
  visible: false,
  title: '',
  orderId: '',
  deliverId: '',
  receiveAll: true,
})

const showDialog = computed({
  get() {
    return props.visible
  },
  set(val) {
    proxy.$emit('update:visible', val) // 更新父组件visible
  },
})
function returnBcak() {
  getMain()
  getList()
}
function receiving() {
  //收货
  modifyDeliverInfo.value.deliverId = infoList.value.data.id
  modifyDeliverInfo.value.orderId = ''
  modifyDeliverInfo.value.visible = true
  modifyDeliverInfo.value.title = '新增收货单'
  modifyDeliverInfo.value.receiveAll = true
  modifyDeliverInfo.value.receivingSourceOrderNo = infoList.value.orderNo
}
function getList() {
  tableInfo.value.refresh = Math.random()
}
function getMain() {
  getDeliverOrderById(props.id).then((res) => {
    if (res && res.success) {
      infoList.value.data = res.data
    }
  })
}
function printProductCode() {
  let ids = infoList.value.data.id
  deliverOrderProductCodePrint([ids]).then((resp) => {
    if (resp && resp.success) {
      printerInfo.value.visible = true
      printerInfo.value.data = resp.data
    }
  })
}
function print() {
  let printRows = []
  let info = infoList.value.data
  let titles = []
  let fieldList = tableInfo.value.fieldList
  let attrLable = []
  fieldList.forEach((it) => {
    let dataTemp = {
      title: it.label,
      align: 'center',
    }
    titles.push(dataTemp)
    attrLable.push(it.value)
  })
  titles[9].width = 7
  titles[10].width = 7
  for (let p in tableInfo.value.data) {
    let attrs = []
    attrLable.forEach((it) => {
      if (
        it == 'qty' ||
        it == 'receiveQty' ||
        it == 'refuseQty' ||
        it == 'sumAmount'
      ) {
        attrs.push(tableInfo.value.data[p][it] || 0)
      } else {
        attrs.push(tableInfo.value.data[p][it] || '')
      }
    })
    printRows.push(attrs)
  }
  proxy.$print.printOrder(
    {
      title: '发货单',
      qrCode:
        (info.purchNo || '') +
        '-' +
        (info.orderNo || '') +
        '-' +
        (info.invoiceNumber || '') +
        '-' +
        (info.waybillNumber || ''),
      sign: ['科室主任', '经手人'],
      orgName: info.createUserName,
      main: {
        发货单号: info.orderNo || '',
        销售单号: info.saleNo || '',
        采购单号: info.purchNo || '',
        发票号码: info.invoiceNumber || '',
        货单号码: info.waybillNumber || '',
        状态: info.statusValue || '',
        客户: info.hospitalName || '',
        预计发货日期: info.expectedDeliverTime || '',
        收货地址: info.receiveAddress || '',
        收货联系人: info.receiveContacts || '',
        联系人电话: info.receivePhone || '',
        创建人: info.createUserName || '',
        创建时间: info.createTime || '',
        备注: info.remark || '',
      },
      sum: [7, 8, 9, 10],
      titles: titles || [],
      rows: printRows,
    },
    'Horizontal'
  )
}
function excel() {}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
function shipment() {
  let ids = [props.id]
  deliverOrderValidateLicense(ids).then((resp) => {
    if (resp && resp.success) {
      shipmentsGoods(ids).then((res) => {
        if (res && res.success) {
          proxy.$emit('returnDetail')
          showDialog.value = false
        }
      })
    }
  })
}
function invalid() {
  deliverOrderDetailInvalid([props.id]).then((res) => {
    if (res && res.success) {
      proxy.$emit('returnDetail')
      showDialog.value = false
    }
  })
}
function printerCallBack(printer) {
  if (printer !== false) {
    proxy.$print.buildRenderBarCode(printerInfo.value.data, printer)
  }
  printerInfo.value.visible = false
}

onMounted(() => {
  if (props.id) {
    getMain()
    getList()
  }
})
</script>
<style scoped lang="scss">
:deep(.table-scroll-hidden) > .el-table {
  overflow-y: hidden;
}
:deep(.el-dialog__body) {
  height: 355px;
  display: flex;
  flex-direction: column;
}
</style>
